﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TCPIPServer;

namespace Communication
{
    public partial class Form1 : Form
    {
        private commServer TCPServer = null;
        private string _receivedData = "";

        public Form1()
        {
            InitializeComponent();
            TCPServer = new commServer();
            TCPServer.TransmissionReceived += dataReceived;
            TCPServer.StartListening(9470);
        }

        private void dataReceived(string data)
        {
            this._receivedData = data;
            this.PublishMessageInControl();
        }

        private void PublishMessageInControl()
        {
            if (this.InvokeRequired)
                this.Invoke(new MethodInvoker(PublishMessageInControl));
            else
            {
                textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + this._receivedData;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.TCPServer.StopListening();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            commServer.broadcast(textBox2.Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }
    }
}
